﻿//****************************************************************************
// Copyright (c) Microsoft Corporation. All rights reserved.
//****************************************************************************
using System;
using System.Configuration;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;
using System.Diagnostics;
using Microsoft.FxCop.Sdk;
using System.Diagnostics.CodeAnalysis;

namespace Microsoft.FxCop.Rules.ASP.Security.Configuration
{
    /// <summary>
    /// Base configuration introspection rule class.
    /// </summary>
    public abstract class ConfigurationIntrospectionRule : SemanticIntrospectionRule
    {
        private string m_sourceFile;
        private const string ConfigFileSearchPattern = "*.config";

        /// <summary>
        /// Initializes a new instance of the <see cref="T:ConfigurationIntrospectionRule"/> class.
        /// </summary>
        /// <param name="name">The name.</param>
        protected ConfigurationIntrospectionRule(string name, string resourceName) : base(name, resourceName) { }

        /// <summary>
        /// Initializes a new instance of the <see cref="T:ConfigurationIntrospectionRule"/> class.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="resourceName">Name of the resource.</param>
        /// <param name="resourceAssembly">The resource assembly.</param>
        protected ConfigurationIntrospectionRule(string name, string resourceName, Assembly resourceAssembly)
            : base(name, resourceName, resourceAssembly)
        {
        }

        /// <summary>
        /// Gets the configuration source file.
        /// </summary>
        /// <value>The source file.</value>
        public string SourceFile
        {
            get { return m_sourceFile; }
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException("value");
                }
                // update the m_sourceFile in case it's not in the system temp folder.
                if (!value.StartsWith(Path.GetTempPath(), StringComparison.OrdinalIgnoreCase))
                {
                    m_sourceFile = value;
                }
            }
        }

        /// <summary>
        /// Checks the specified configuration.
        /// </summary>
        /// <param name="configuration">The configuration.</param>
        /// <returns></returns>
        protected abstract Microsoft.FxCop.Sdk.ProblemCollection Check(System.Configuration.Configuration configuration);

        protected void AddProblem(string configFile, int lineNumber, string issueData)
        {
            Problem problem;

            if (string.IsNullOrEmpty(issueData))
                problem = new Problem(base.GetResolution(), configFile, lineNumber);
            else
                problem = new Problem(base.GetResolution(new string[] { issueData }), configFile, lineNumber);

            base.Problems.Add(problem);
        }

        protected void AddProblem(ElementInformation elementInfo, string issueData)
        {
            string configFile = string.Empty;
            int lineNumber = 0;

            if (elementInfo != null)
            {
                configFile = elementInfo.Source;
                lineNumber = elementInfo.LineNumber;
            }

            AddProblem( configFile,
                        lineNumber,
                        issueData);
        }

        protected void AddProblem(ElementInformation elementInfo)
        {
            AddProblem(elementInfo, string.Empty);
        }
    }
}
